#include "cClient.h"
#include "../Forms/Form1.h"



cClientThread::cClientThread(Form1 * formulaire,cSocket * client):
                        _formulaire(formulaire),
                        _client(client)
{
    InitializeCriticalSection(&_sectioncritique);
    //ctor
}

cClientThread::~cClientThread()
{
    DeleteCriticalSection(&_sectioncritique);
    //dtor
}

void cClientThread:: doJob()
{
        int index;
            bool fini=false;
            while(!fini)
            {
                string client=_client->Receive();
                if(_client->_socket!=INVALID_SOCKET)
                {
                    struct tm Today;
                    time_t  * maintenant=new time_t;

                    time(maintenant);
                    Today = *localtime(maintenant);

                    char * datetime=new char[9];

                    sprintf(datetime,"%2.2d:%2.2d:%2.2d",Today.tm_hour,Today.tm_min,Today.tm_sec);

                    EnterCriticalSection(&_sectioncritique);

                    time_t maintenant2;
                    index=_formulaire->listBox1->TrouverLigne(0,client);
                    if(index==-1)
                        _formulaire->listBox1->InsererLigne(2,client.c_str(),datetime);
                    else
                    {
                        _formulaire->listBox1->SetCellText(index,1,datetime,maintenant);
                    }

                    LeaveCriticalSection(&_sectioncritique);

                }
                else
                {
                    delete _client;
                    fini=true;
                }
            }

}
